function PyResult() {
    this.form = this.InitForm();
    this.table = this.InitTableByBt();
}

PyResult.prototype.InitForm = function () {
    var form;

    $("#year").val($("#hidyear").val());
    layui.use('form', function () {
        form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
        form.render();
    });
    return form;
}
var _table;
PyResult.prototype.InitTableByBt = function () {
    var table;
    var _this = this;
    var index = layer.load();
    layui.use('table', function () {
        table = layui.table;
        table.init('uatmzpdsummarylist', {
            height: $(window).height() - 100
            , url: 'get_passno_list' // 数据接口
            , page: false // 开启分页
            // ,limit : 30
            , id: 'uatmzpdsummary'
            , method: "post"
            , request: {
                type: 'post'
            }
            , where: {
                year: $("#year").val(),
                quarter: $('input[name="quarter"]:checked ').val(),
                orgid: $("#szdw").val()
            }
            , done: function (res, page, count) {

                if (res.data) {
                    for (var i = 0; i < res.data.length; i++) {
                        if (i == 0) {

                            if ($("#szdw").val() != 19) {
                                window.status = res.data[i].status;
                            } else {
                                window.status = undefined;
                            }

                            // if (res.data[i].status == 0 && $('#userType').val() == 3) {
                            //     $("[data-field='reason']").children().each(function (index) {
                            //         $(this).text('');
                            //     })
                            //
                            //     $("[data-field='howtodo']").children().each(function (index) {
                            //         $(this).text('');
                            //     })
                            // }


                        }


                    }
                }


                var year = $("#year").val();
                var quarter = $('input[name="quarter"]:checked ').val();
                var orgname = $("#szdw").find("option:selected").text();
                var orgname = $.trim(orgname);
                $("#title").html("(" + orgname + "）单位" + year + "年第" + quarter + "季度“基本合格”、“不合格”党员基本情况统计分析表");
                console.log($("[data-field='birthday']"));
                $("[data-field='birthday']").children().each(function (index) {
                    if (index > 0) {
                        var text = $(this).text();
                        if (text != "" && text != null) {
                            console.log(text);
                            var begin = (new Date(Date.parse(text.replace(/-/g, "/")))).format("yyyy年MM月");
                            console.log(begin);
                            $(this).text(begin);
                        }
                    }
                })
                $("[data-field='workTime']").children().each(function (index) {
                    if (index > 0) {
                        var text = $(this).text();
                        if (text != "" && text != null) {
                            var begin = (new Date(Date.parse(text.replace(/-/g, "/")))).format("yyyy年MM月");
                            $(this).text(begin);
                        }
                    }
                })
                $("[data-field='joinpartytime']").children().each(function (index) {
                    if (index > 0) {
                        var text = $(this).text();
                        if (text != "" && text != null) {
                            var begin = (new Date(Date.parse(text.replace(/-/g, "/")))).format("yyyy年MM月");
                            $(this).text(begin);
                        }
                    }
                })
                $("[data-field='sex']").children().each(function (index) {
                    var text = $(this).text();
                    if (text == "0") {
                        $(this).text('男')
                    }
                    else if (text == "1") {
                        $(this).text('女')
                    }
                })
                $("[data-field='worklevel']").children().each(function (index) {
                    var text = $(this).text();

                    if (text == "1") {
                        $(this).text('处级领导干部')
                    }
                    else if (text == "2") {
                        $(this).text('科级领导干部')
                    } else if (text == "3") {
                        $(this).text('民警')
                    } else if (text == "4") {
                        $(this).text('未定职务')
                    } else if (text == "5") {
                        $(this).text('领导班子')
                    }
                })


                layer.close(index);
            }
        });
    })
    _table = table;
    return table;
}


PyResult.prototype.searchtable = function () {
    _table.reload('uatmzpdsummary', {

        where: {
            year: $("#year").val(),
            quarter: $('input[name="quarter"]:checked ').val(),
            szdw: $("#szdw").val(),
            orgid: $("#szdw").val()
        }
    });
}


PyResult.prototype.excle = function () {
    top.ShowZheZhao();
    var tabledataJson = this.table.cache;
    var year = $("#year").val();
    var quarter = $('input[name="quarter"]:checked ').val();
    var orgname = $.trim($("#szdw").find("option:selected").text());
    tabledataJson['yearid'] = year;
    tabledataJson['quarter'] = quarter;
    tabledataJson['orgname'] = orgname;
    tabledataJson = JSON.stringify(tabledataJson);
    tabledataJson = tabledataJson.replace(/LAY_TABLE_INDEX/g, 'lay_TABLE_INDEX');
    $.ajax({
        type: "POST",
        url: "export_passno_mzpd",
        contentType: 'application/json',
        data: tabledataJson,
        beforeSend: function (XHR) {

        },
        success: function (data) {
            data = eval('(' + data + ')');
            if (data.result > 0) {
                //top.mylayui.msg('导出成功', {icon: 1});
                //document.location.reload();
                window.open(data.msg, '_self');
            }
            else {
                top.mylayui.msg(data.msg, {icon: 0});
            }
            top.ShowZheZhao();
        },
        complete: function (XHR) {
            top.mylayui.closeAll('loading');
        },
        error: function () {

        }
    });
}

PyResult.prototype.save = function () {
    var tabledataJson = JSON.stringify(this.table.cache);
    var year = $("#year").val();
    var quarter = $('input[name="quarter"]:checked ').val();
    var orgid = $("#szdw").val();
    tabledataJson = tabledataJson.replace(/LAY_TABLE_INDEX/g, 'lay_TABLE_INDEX');
    var uatmzpdsummary = JSON.parse(tabledataJson);
    uatmzpdsummary['yearid'] = year;
    uatmzpdsummary['quarter'] = quarter;
    uatmzpdsummary['orgid'] = orgid;
    tabledataJson = JSON.stringify(uatmzpdsummary);
    console.log(tabledataJson);
    $.ajax({
        type: "POST",
        url: "mzpd_passno_save",
        contentType: 'application/json',
        data: tabledataJson,
        beforeSend: function (XHR) {

        },
        success: function (data) {
            data = eval('(' + data + ')');
            if (data.result > 0) {
                top.mylayui.msg(data.msg, {icon: 1});
                document.location.reload();
            }
            else {
                top.mylayui.msg(data.msg, {icon: 0});
            }
        },
        complete: function (XHR) {
            var res = XHR.responseText;
            closeD(res)
        },
        error: function (XHR, errorText, errorType) {
            var res = XHR.responseText;
            closeD(res, errorText)
        }
    });
}

function closeD(result) {
    if (result > 0) {
        top.mylayui.msg('操作成功', {icon: 1});
        top.mylayui.closeAll('iframe');
        vPassNoUATMzpd.searchtable();
    }
    else {
        top.mylayui.msg('操作失败', {icon: 0});
    }
}

function OpenUserInfo(mzpdpassnoid,cardno,orgid) {

    top.mylayui.open({
        type: 2,
        title: '编辑',
        shadeClose: true,
        shade: 0.5,
        area: ['900px', '700px'],
        content: 'useruatmzpdSeason/editMzpd?mzpdId=' + mzpdpassnoid+"&year="+$('#year').val()+"&quarter="+$('input[name="quarter"]:checked ').val()+
        "&orgid="+orgid+"&cardno="+cardno
    });

}


PyResult.prototype.updateStatus = function (status) {

    if (window.status == undefined || window.status == 'undefined') {
        layer.msg('请选择部门、', {icon: 0});
        return;
    }

    if (status == 2) {
        if (window.status != 1) {
            layer.msg('该部门未提交', {icon: 0});
            return;
        }

    }

    if (status == 1) {

        if (window.status != 0) {
            layer.msg('以提交或归档，无法提交', {icon: 0});
            return;
        }

    }

    if (status == 0) {

        if (window.status != 1) {
            layer.msg('该部门未提交', {icon: 0});
            return;
        }

    }


    var data = {
        year: $("#year").val(),
        quarter: $('input[name="quarter"]:checked ').val(),
        orgid: $("#szdw").val(),
        status: status
    }

    $.ajax({
        type: "get",
        url: "updateStatus",

        data: data,
        beforeSend: function (XHR) {
            top.ShowZheZhao();
        },
        success: function (data) {
            layer.msg('操作成功', {icon: 1})


            top.ShowZheZhao();
        },
        complete: function (XHR) {
            top.mylayui.closeAll('loading');
        },
        error: function () {
            layer.msg('操作失败', {icon: 0})
            top.ShowZheZhao();

        }
    });


}


var vPassNoUATMzpd = null;
$(function () {
    vPassNoUATMzpd = new PyResult();

    if ($('#userType').val() == 3) {

        $('.admin').show();
        $('.user').hide();
    } else {

        $('.admin').hide();
        $('.user').show();


    }
})